//
//  TDWeiZhuanViewController.m
//  TFB
//
//  Created by yull on 15/12/21.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDWeiZhuanViewController.h"
#import "TDMerchantCell.h"
#import "TDMerchant.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>

@interface TDWeiZhuanViewController () <UITableViewDataSource>

@property (nonatomic, strong) UIButton *currentTypeBtn1;
@property (nonatomic, strong) UIButton *currentTypeBtn2;
@property (nonatomic, strong) NSMutableArray *merchantArray;

@end

@implementation TDWeiZhuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"微赚";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tixianBtn.layer.borderColor = [UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1].CGColor;
    self.tixianBtn.layer.borderWidth = 1;
    self.tixianBtn.layer.cornerRadius = 5;
    self.tixianBtn.layer.masksToBounds = YES;
    
    [self.accountBtn addTarget:self action:@selector(button_type:) forControlEvents:UIControlEventTouchUpInside];
    [self.merchantBtn addTarget:self action:@selector(button_type:) forControlEvents:UIControlEventTouchUpInside];
    [self.noticeBtn addTarget:self action:@selector(button_type:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.xiaoerBtn addTarget:self action:@selector(button_bottom:) forControlEvents:UIControlEventTouchUpInside];
    [self.zhangguiBtn addTarget:self action:@selector(button_bottom:) forControlEvents:UIControlEventTouchUpInside];
    [self.shangrenBtn addTarget:self action:@selector(button_bottom:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.merchantTableView registerNib:[UINib nibWithNibName:@"TDMerchantCell" bundle:nil] forCellReuseIdentifier:@"MerchantCell"];
    self.merchantTableView.rowHeight = 58;
    
    UITextView *footerView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, SVFS.width, 60)];
    footerView.editable = NO;
    footerView.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5);
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    footerView.textColor = [UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1];
    footerView.text = @"备注：加入代理商及合伙人享受全部下级商户收入";
    footerView.font = [UIFont systemFontOfSize:15];
    self.merchantTableView.tableFooterView = footerView;
    
    [self button_bottom:self.xiaoerBtn];
    
    [self initData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.bottomView.hidden = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)initData {
    
    TDMerchant *merchant1 = [[TDMerchant alloc] init];
    merchant1.merchantName = @"小二数量";
    TDMerchant *merchant2 = [[TDMerchant alloc] init];
    merchant2.merchantName = @"掌柜数量";
    TDMerchant *merchant3 = [[TDMerchant alloc] init];
    merchant3.merchantName = @"商人数量";
    TDMerchant *merchant4 = [[TDMerchant alloc] init];
    merchant4.merchantName = @"总下级商户数量";
    self.merchantArray = [NSMutableArray arrayWithArray:@[merchant1, merchant2, merchant3, merchant4]];
    
    [TDHttpEngine requestFor_GETMYMERCHANT_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] complete:^(BOOL succeed, NSString *msg, id reqBody) {
        if (succeed) {
            
            TDMerchant *merchant1 = self.merchantArray[0];
            merchant1.merchantCount = reqBody[@"level1Count"];
            merchant1.merchantAmt = reqBody[@"level1Amt"];
            TDMerchant *merchant2 = self.merchantArray[1];
            merchant2.merchantCount = reqBody[@"level2Count"];
            merchant2.merchantAmt = reqBody[@"level2Amt"];
            TDMerchant *merchant3 = self.merchantArray[2];
            merchant3.merchantCount = reqBody[@"level3Count"];
            merchant3.merchantAmt = reqBody[@"level3Amt"];
            TDMerchant *merchant4 = self.merchantArray[3];
            merchant4.merchantCount = reqBody[@"allCount"];
            merchant4.merchantAmt = reqBody[@"allAmt"];
            
            [self.merchantTableView reloadData];
        }
        else
        {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (IBAction)button_commend:(id)sender {
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
#ifdef SHARE_URL_NEED_ID
    NSString *shareText = [NSString stringWithFormat:@"我用的“%@”手机快捷支付非常好用，现在推荐给你使用，快打开网址下载吧，%@%@", APPNAME, SHARE_URL, [[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]];
#else
    NSString *shareText = [NSString stringWithFormat:@"我用的“%@”手机快捷支付非常好用，现在推荐给你使用，快打开网址下载吧，%@", APPNAME, SHARE_URL];
#endif
    [shareParams SSDKSetupShareParamsByText:shareText
                                     images:nil
                                        url:nil
                                      title:nil
                                       type:SSDKContentTypeText];
    
    
    [ShareSDK showShareActionSheet:self.view
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                           
                       case SSDKResponseStateBegin:
                       {
                           [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                           break;
                       }
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           if (platformType == SSDKPlatformTypeSMS && [error code] == 201)
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:@"失败原因可能是：1、短信应用没有设置帐号；2、设备不支持短信应用；3、短信应用在iOS 7以上才能发送带附件的短信。"
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           else
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           break;
                       }
                       default:
                           break;
                   }
                   
                   if (state != SSDKResponseStateBegin)
                   {
                       [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                   }
                   
               }];
}

- (void)button_type:(UIButton *)button {
    
    self.bottomView.hidden = YES;
    
    if (button == self.currentTypeBtn1) {
        return;
    }
    
    if (button == self.accountBtn || button == self.noticeBtn) {
        self.blankView.hidden = NO;
        self.merchantTableView.hidden = YES;
    } else {
        self.blankView.hidden = YES;
        self.merchantTableView.hidden = NO;
    }
}

- (void)button_bottom:(UIButton *)button {
    
    if (button == self.currentTypeBtn2) {
        return;
    }
    
    self.currentTypeBtn2.backgroundColor = [UIColor clearColor];
    [self.currentTypeBtn2 setTitleColor:[UIColor whiteColor] forState:0];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitleColor:[UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1] forState:0];
    
    [self  setupBottomView:button.tag - 210];
}

- (void)setupBottomView:(NSInteger)type {
    
    if (type == 0) {
        self.rateLabel.text = @"0.48%";
        
        NSMutableAttributedString *shouyiStr = [[NSMutableAttributedString alloc] initWithString:@"无\n" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        [shouyiStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"（成功推荐3人升级为掌柜，可享受万/3元的收益！）" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1]}]];
        self.shouyiLabel.attributedText = shouyiStr;
        
        self.illustrateView.hidden = YES;
        
//        [self.shouyiLabel sizeToFit];

        CGRect frame = self.commendBtn.frame;
        frame.origin.y = CGRectGetMaxY(self.shouyiLabel.frame) + 10;
        self.commendBtn.frame = frame;
        
        self.bottomScrollView.contentSize = CGSizeMake(1, CGRectGetMaxY(self.commendBtn.frame)+10);
        
        self.currentTypeBtn2 = self.xiaoerBtn;
    } else if (type == 1) {
        self.rateLabel.text = @"0.45%";
        
        NSMutableAttributedString *shouyiStr = [[NSMutableAttributedString alloc] initWithString:@"享受旗下小二的3元/万刷卡金\n" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        [shouyiStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"（继续！成功推荐20人成为商人，可享受三重费率差收益喔！）" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1]}]];
        self.shouyiLabel.attributedText = shouyiStr;
        
        self.illustrateView.hidden = NO;

        NSMutableAttributedString *illustrateStr = [[NSMutableAttributedString alloc] initWithString:@"假设您推荐5人，每人推荐5人，合计25人，月交易5万/人\n年收益：3元/万*25人*月交易5万*12个月=4500元" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        self.illustrateLabel.attributedText = illustrateStr;
        
        CGRect adjustRect = self.shouyiLabel.frame;
        adjustRect.size.height = 1000;
        adjustRect.size.width = SVFS.width - 53;

        self.shouyiLabel.frame = [self.shouyiLabel textRectForBounds:adjustRect limitedToNumberOfLines:0];
        adjustRect.origin = self.illustrateLabel.frame.origin;
        self.illustrateLabel.frame = [self.illustrateLabel textRectForBounds:adjustRect limitedToNumberOfLines:0];
        
        CGRect frame = self.illustrateView.frame;
        frame.origin.y = CGRectGetMaxY(self.shouyiLabel.frame) + 10;
        frame.size.height = CGRectGetMaxY(self.illustrateLabel.frame);
        self.illustrateView.frame = frame;
        frame = self.commendBtn.frame;
        frame.origin.y = CGRectGetMaxY(self.illustrateView.frame) + 10;
        self.commendBtn.frame = frame;
        
        self.bottomScrollView.contentSize = CGSizeMake(1, CGRectGetMaxY(self.commendBtn.frame)+10);
        
        self.currentTypeBtn2 = self.zhangguiBtn;
    } else {
        self.rateLabel.text = @"0.42%";
        
        NSMutableAttributedString *shouyiStr = [[NSMutableAttributedString alloc] initWithString:@"1.商人旗下的小二：6元\n2.掌柜旗下的小二：6-3=3元\n3.商人旗下的掌柜：6-3=3元\n" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        [shouyiStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"加盟便捷支付代理商，享受更多微赚收入\n加盟电话：4008775503" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1]}]];
        self.shouyiLabel.attributedText = shouyiStr;
        
        self.illustrateView.hidden = NO;
        
        NSMutableAttributedString *illustrateStr = [[NSMutableAttributedString alloc] initWithString:@"假设您推荐50人，每人推荐5人，合计250人，月交易5万/人\n" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        [illustrateStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"年收益：\n" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1]}]];
        [illustrateStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"1.6元/万*250*月交易5万*12个月=90000\n2.3元/万*50*月交易5万*12个月=9000\n" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}]];
        [illustrateStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"总计：99000元" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:224.0/225.0 green:56.0/255.0 blue:42.0/255.0 alpha:1]}]];
        self.illustrateLabel.attributedText = illustrateStr;
        
        CGRect adjustRect = self.shouyiLabel.frame;
        adjustRect.size.height = 1000;
        adjustRect.size.width = SVFS.width - 53;
        self.shouyiLabel.frame = [self.shouyiLabel textRectForBounds:adjustRect limitedToNumberOfLines:0];
        adjustRect.origin = self.illustrateLabel.frame.origin;
        self.illustrateLabel.frame = [self.illustrateLabel textRectForBounds:adjustRect limitedToNumberOfLines:0];
        
        CGRect frame = self.illustrateView.frame;
        frame.origin.y = CGRectGetMaxY(self.shouyiLabel.frame) + 10;
        frame.size.height = CGRectGetMaxY(self.illustrateLabel.frame);
        self.illustrateView.frame = frame;
        frame = self.commendBtn.frame;
        frame.origin.y = CGRectGetMaxY(self.illustrateView.frame) + 10;
        self.commendBtn.frame = frame;
        
        self.bottomScrollView.contentSize = CGSizeMake(1, CGRectGetMaxY(self.commendBtn.frame)+10);
        
        self.currentTypeBtn2 = self.shangrenBtn;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.merchantArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TDMerchantCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantCell" forIndexPath:indexPath];
    
    TDMerchant *merchant = self.merchantArray[indexPath.row];
    cell.countTypeLabel.text = merchant.merchantName;
    cell.countLabel.text = [NSString stringWithFormat:@"%lld", merchant.merchantCount.longLongValue];
    cell.shouruLabel.text = merchant.merchantAmt;
    
    return cell;
}

@end
